<template>
    <div style="width: 40%;margin-left: 30%;">
        <!-- <p style="text-align: center"><img src="../assets/images/logo.png" height="55"/></p> -->
        <p style="text-align: center"></p>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>用户注册</span>
            </div>
            <div class="text item">
                <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
                    <el-form-item label="用户名" prop="userName">
                        <el-input v-model="form.userName"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="form.password" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="password2">
                        <el-input v-model="form.password2" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="手机" prop="phone">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="姓名" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" prop="address">
                        <el-input v-model="form.address"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div style="text-align: center">
                <el-button type="primary" @click="registerUser()">注册</el-button>
                <router-link type="primary" to="/login" style="margin-left: 10px;">
                    <el-button type="primary">去登录</el-button>
                </router-link>
            </div>
        </el-card>
    </div>
</template>

<script>
import axios from "axios";
import ElementUI from "element-ui";
import { getByUsername, getByPhone, register } from '@/api/login.js'

export default {
    beforeCreate() {
        document.querySelector('body').setAttribute('style', "background: url('https://mp-036ce09c-e64a-4087-bdff-bccfeedf3941.cdn.bspapp.com/background.jpg');background-size:100%")
    },
    name: 'register',
    data() {
        //检验用户名是否已经存在于数据库中e
        let checkUserName = (rule, value, callback) => {
            getByUsername(value)
                .then(response => {
                    if (response) {
                        callback();
                    } else {
                        callback(new Error('用户名已经存在,请重新输入!'));
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
        };
        //检验手机号是否已经存在于数据库中
        let checkPhone = (rule, value, callback) => {
            getByPhone(value).then(function (response) {
                if (response) {
                    callback();
                } else {
                    callback(new Error('手机号已经被注册,请重新输入!'));
                }
            })
                .catch(function (error) {
                    console.log(error);
                });
        };
        //检验密码和确认密码是否一样
        let checkPassword = (rule, value, callback) => {
            if (value !== this.form.password) {
                callback(new Error('密码和确认密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            form: {
                userName: '',
                password: '',
                password2: '',
                phone: '',
                name: '',
                address: ''
            },
            rules: {
                userName: [
                    { required: true, message: "请输入用户名", trigger: "blur" },
                    { validator: checkUserName, trigger: 'blur' }
                ],
                password: [
                    { required: true, message: "请输入密码", trigger: "blur" }
                ],
                password2: [
                    { required: true, message: "请输入确认密码", trigger: "blur" },
                    { validator: checkPassword, trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: "请输入手机号", trigger: "blur" },
                    { validator: checkPhone, trigger: 'blur' }
                ],
                name: [
                    { required: true, message: "请输入姓名", trigger: "blur" }
                ],
                address: [
                    { required: true, message: "请输入地址", trigger: "blur" }
                ]
            }
        }
    },
    methods: {
        registerUser() {
            this.$refs["form"].validate((valid, msg) => {
                if (valid) {
                    let user = {
                        "username": this.form.userName,
                        "password": this.form.password,
                        "phone": this.form.phone,
                        "address": this.form.address,
                        "name": this.form.name
                    }
                    let _this = this;
                    register(user).then(function (response) {
                        console.log("resp: ", response);
                        let registerSuccess = response;
                        if (registerSuccess) {
                            ElementUI.Message.success("注册成功！！");
                            _this.$router.push("/");
                        }
                    })
                        .catch(function (error) {
                            console.log(error);
                            ElementUI.Message.error("注册失败！！");
                        })
                } else {
                    ElementUI.Message.error("你填写的信息不正确！！");
                    return false;
                }
            });
        }
    }
}
</script>

<style scoped></style>
